//
//  OCSSHomePage.swift
//  OCSSStoreSwift
//
//  Created by MacMiniKangChe on 2018/10/11.
//  Copyright © 2018年 MacMiniKangChe. All rights reserved.
//

import UIKit
import SwiftyJSON
import HandyJSON


class OCSSHomePage: OCSSBaseRefreshViewController, UITableViewDelegate,UITableViewDataSource {

    var hasNoti:NSInteger = 1;
    var dateId:String = "JinRi";
    var orderData:NSMutableArray?;
    
    
    lazy var tbHeaderView: OCSSHomeTableHeaderView! = {
        let headerView:OCSSHomeTableHeaderView = Bundle.main.loadNibNamed("OCSSHomeTableHeaderView", owner: self, options: nil)?.last as! OCSSHomeTableHeaderView;
        headerView.paraVC = self;
        return headerView;
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        orderData = NSMutableArray.init();
        self.setNavBtn(title: "user-nav-normal-h.png", isRight: false);
        NotificationCenter.default.addObserver(self, selector: #selector(requestWithDate(notification:)), name: NSNotification.Name(rawValue: "DateSelectNotifacation"), object: nil);
        tableView.tableHeaderView = tbHeaderView;
        tableView.tableFooterView = UIView();
        self.regTableViewNib(tableView: tableView, className:OCSSNotificationCell.className());
        self.regTableViewNib(tableView: tableView, className: OCSSHomeTableViewCell.className());
        //请求销售数据
        requestForSaleData();
        //请求订单数据
        requestForOrderData();
    }
//    MARK:ButtonEvent
    override func barButtonEvent(button: UIBarButtonItem) {
        if button.tag == 11 {
            let userCenterPage = OCSSUserCenterPage.init();
           self.navigationController?.pushViewController(userCenterPage, animated: true);
        }
    }
    //下拉刷新
    override func refresh() {
        self.page = 1;
        requestForSaleData();
    }
    //上拉加载更多
    override func loadMore() {
        self.page += 1;
        requestForOrderData();
    }
    //    MARK:通知
    @objc func requestWithDate(notification:NSNotification) -> Void {
        dateId = notification.object as! String;
        requestForSaleData();
    }
    //MARK:Request
    func requestForSaleData() -> Void {
        let paras:[String: String] = ["DataNum":dateId];
        OCSSNetRequest.requestData(url: "/Home/GetMobileCount", para: paras, type: MethodType.post, success: { (result) in
            let model = JSONDeserializer<OCSSBaseModel>.deserializeFrom(json:JSON(result).rawString());
            self.tbHeaderView.headerInfo = model?.Data;
        }) { (error) in
            print(error);
        }
    }
    func requestForOrderData() -> Void {
        let paras:[String: String] = ["pageIndex":String(self.page)];
        OCSSNetRequest.requestData(url: "/Home/GetOrderList", para: paras, type: MethodType.post, success: { (result) in
            let resDic = JSON(result).dictionaryObject;
            let arr = JSON(resDic!["Data"]!).arrayObject as! [[String:Any]];
            let datas = [OCSSHomeModel].deserialize(from: arr);
            if ((datas?.count) != 0 && self.page == 1) {
                self.orderData?.removeAllObjects();
            }
            self.orderData?.addObjects(from: datas! as [Any]);
            
            self.tableView.reloadData();
            
        }) { (error) in
            print(error);
        }
    }
    //MARK:UITbleViewDelegate&&UITableViewDataSource
    func numberOfSections(in tableView: UITableView) -> Int {
        return hasNoti+1;
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if (hasNoti == 1 && section == 0) {
            return 1;
        }else{
            return (self.orderData?.count)!;
        }
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if (hasNoti == 1 && indexPath.section == 0) {
            let cell = tableView.dequeueReusableCell(withIdentifier: OCSSNotificationCell.className()) as! OCSSNotificationCell;
            cell.selectionStyle = UITableViewCellSelectionStyle.none;
            
            return cell
        }else{
            let cell:OCSSHomeTableViewCell = tableView.dequeueReusableCell(withIdentifier: OCSSHomeTableViewCell.className()) as! OCSSHomeTableViewCell;
            cell.selectionStyle = UITableViewCellSelectionStyle.none;
            cell.model = self.orderData?[indexPath.row] as? OCSSHomeModel;
            
            return cell;
        }
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if hasNoti == 1 && indexPath.section == 0 {
            return 48;
        }
        return 68;
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let model = self.orderData?[indexPath.row] as? OCSSHomeModel;
        
        if hasNoti==1 && indexPath.section == 0 {
            
        }else{
            let orderDetailPage = OCSSOrderDetailPage.init();
            orderDetailPage.hidesBottomBarWhenPushed = true;
            if (model?.DocumentNmae?.hasSuffix("出库"))!{
                orderDetailPage.type = orderType.outOrder;
            }
            else if (model?.DocumentNmae?.hasSuffix("入库"))!{
                orderDetailPage.type = orderType.putInOrder;
            }
            orderDetailPage.orderId = model?.Id;
            navigationController?.pushViewController(orderDetailPage, animated: true);
            
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
